/**
 * Filename: AdviceActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/3 10:35
 */
package com.github.postsanf.yinian.activity;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.ClipboardManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.ISkinUpdate;
import com.github.postsanf.yinian.entity.YNCommonResponse;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.github.postsanf.yinian.utils.SkinPackageManager;
import com.github.postsanf.yinian.utils.SnackBarUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.LinkedHashMap;

/*
打小报告
 */
public class AdviceActivity extends BaseActivity implements View.OnClickListener, ISkinUpdate {
    private Toolbar mToolbar;
    private RadioGroup rg_advice;
    private LinearLayout tv_advice_copy_wx;
    private EditText et_write_status;
    private TextView iv_phone_info;
    private String mPhoneSystem;
    private String mPhoneVersion;
    private int advice_which = 2;
    private String advice_content;
    private RadioButton rb_advice_bug;
    private RadioButton rb_advice_product;
    private LinearLayout tv_advice_copy_qq;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_advice);
        BaseApplication.getInstance().mActivitys.add(this);
        initView();
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.advice_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_return);
        et_write_status = (EditText) findViewById(R.id.et_write_status);
        tv_advice_copy_wx = (LinearLayout) findViewById(R.id.tv_advice_copy_wx);
        iv_phone_info = (TextView) findViewById(R.id.iv_phone_info);
        rg_advice = (RadioGroup) findViewById(R.id.rg_advice);
        rb_advice_bug = (RadioButton) findViewById(R.id.rb_advice_bug);
        rb_advice_product = (RadioButton) findViewById(R.id.rb_advice_product);
        tv_advice_copy_qq = ((LinearLayout) findViewById(R.id.tv_advice_copy_qq));

        mPhoneSystem = android.os.Build.MODEL;
        mPhoneVersion = android.os.Build.VERSION.RELEASE;
        // 获取手机信息
        iv_phone_info.setText("#" + mPhoneSystem + "#" + mPhoneVersion);
        tv_advice_copy_wx.setOnClickListener(this);
        tv_advice_copy_qq.setOnClickListener(this);
    }

    private boolean checkInfo() {
        advice_content = et_write_status.getText().toString().trim();
        if (advice_which > 1) {
            SnackBarUtils.makeShort(mToolbar, "请选择反馈类型").show();
            return false;
        }
        if (advice_content.length() == 0) {
            SnackBarUtils.makeShort(mToolbar, "反馈内容不能为空").show();
            return false;
        }
        return true;
    }

    // 提交反馈意见
    private void submitAdvice() {
        if (checkInfo()) {
            LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
            mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
            mParam.put(CommonConstants.YNS_INFO_TYPE, String.valueOf(advice_which));
            mParam.put(CommonConstants.YNS_CONTENT, advice_content);
            mParam.put(CommonConstants.YNS_MACHINE, mPhoneVersion);
            mParam.put(CommonConstants.YNS_SYSTEM, mPhoneSystem);
            StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynProductAdvice)).setMethod(HttpMethods.Post).setParamMap(mParam)
                    .setHttpListener(new HttpListener<String>(true, false, true) {
                        @Override
                        public void onSuccess(String s, Response<String> response) {
                            YNCommonResponse resAdvice = gson.fromJson(s, YNCommonResponse.class);
                            if (ReqUtils.isSuccess(resAdvice.getCode())) {
                                showToast("反馈成功");
                                finish();
                            }
                        }

                        @Override
                        public void onFailure(HttpException e, Response<String> response) {
                            showToast(CommonConstants.YN_NET_LINK);
                        }
                    });
            liteHttp.executeAsync(req);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_advice_copy_wx:
                ClipboardManager clip = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                clip.setText("yinian0230");
                SnackBarUtils.makeShort(mToolbar, getString(R.string.advice_copy_wx)).show();
                break;
            case R.id.tv_advice_copy_qq:
                ClipboardManager clip1 = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                clip1.setText("473408401");
                SnackBarUtils.makeShort(mToolbar, "QQ号已复制，快去后台反馈宝贵的意见吧").show();
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_advice, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            case R.id.action_submit:
                switch (rg_advice.getCheckedRadioButtonId()) {
                    case R.id.rb_advice_bug:
                        advice_which = 0;
                        break;
                    case R.id.rb_advice_product:
                        advice_which = 1;
                        break;
                    default:
                        advice_which = 2;
                }
                submitAdvice();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void updateTheme() {
        if (mToolbar != null) {
            try {
                Resources mResource = SkinPackageManager.getInstance(this).mResources;
                mToolbar.setNavigationIcon(mResource.getDrawable(mResource.getIdentifier("ic_return", "drawable", "com.github.postsanf.skinby")));
                mToolbar.setBackgroundColor(mResource.getColor(mResource.getIdentifier("yncolor", "color", "com.github.postsanf.skinby")));
                mToolbar.setTitleTextColor(mResource.getColor(mResource.getIdentifier("border_color", "color", "com.github.postsanf.skinby")));
                rb_advice_product.setButtonDrawable(mResource.getDrawable(mResource.getIdentifier("radio_sel", "drawable", "com.github.postsanf.skinby")));
                rb_advice_bug.setButtonDrawable(mResource.getDrawable(mResource.getIdentifier("radio_sel", "drawable", "com.github.postsanf.skinby")));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApplication.getInstance().mActivitys.remove(this);
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        if (SkinPackageManager.getInstance(this).mResources != null) {
            updateTheme();
        }
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
